/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.ufrgs.inf.prav.interop.jsf.components;

import br.ufrgs.inf.prav.interop.util.PathParser;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Fernando
 */
public class NavBarComponent extends PravBaseComponent{

    private String location;

    public static final String COMPONENT_FAMILY = "PRAV_NAVBAR";
    public static final String COMPONENT_TYPE = "PRAV_NAVBAR";

    public NavBarComponent(){
        
    }

    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        Map<String, Object> params = context.getViewRoot().getAttributes();

        System.out.println("keys:");
        for (String key : params.keySet()){
//            System.out.println(key + ": " + params.get(key));
        }
        System.out.println("\n\n");

        HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();
        String defaultPath = request.getPathInfo();
        String modifier = PathParser.getPathFromPath1ToPath2(defaultPath, location);
        context.getExternalContext().getRequestMap().put("barLocModifier", modifier);

        System.out.println("modifier: " + modifier);


        try {

            UIViewRoot vr = (UIViewRoot)this.getParent();
//            System.out.println("getRequestContextPath: " + context.getExternalContext().getRequestContextPath());
//            System.out.println("getRequestServletPath: " + context.getExternalContext().getRequestServletPath());
//            System.out.println("getRequestPathInfo: " + context.getExternalContext().getRequestPathInfo());
//
//            System.out.println("getRequestPathInfo In: " + this.getFacesContext().getExternalContext().getRequestPathInfo());
//
//            System.out.println("View: " + vr.getViewId() + " - ");
//            if (vr.getParent() == null)
//                System.out.println("view's parent = NULL");

           List<UIComponent> children = vr.getChildren();
            for (UIComponent kid : children) {
//                System.out.println("id: " + kid.getId() +
//                        " - family: " + kid.getFamily() +
//                        " - clientId: " + kid.getClientId(context));
            }

            HttpServletRequest req = (HttpServletRequest)context.getExternalContext().getRequest();
//            System.out.println("Request:");
//            System.out.println("getContextPath: " + req.getContextPath());
//            System.out.println("getPathInfo: " + req.getPathInfo());
//            System.out.println("getPathTranslated: " + req.getPathTranslated());
//            System.out.println("getQueryString: " + req.getQueryString());
//            System.out.println("getRequestedSessionId: " + req.getRequestedSessionId());
//            System.out.println("getRequestURL: " + req.getRequestURL());
//            System.out.println("getServletPath: " + req.getServletPath());


//            System.out.println("Parent: " +
//                     " - " + this.getParent().getFamily());
//            System.out.println("Parent's parent: " +
//                     " - " + this.getParent().getParent().getFamily());
//            System.out.println("Parent's parent's parent: " +
//                     " - " + this.getParent().getParent().getParent().getFamily());

        } catch (Exception e) {
        }
//        String prev = (String)params.get("previous");
        

        String target = (String)params.get("target");
        if (target == null)
            target = "";
        this.setDisplayPlataform(target);
        if (!displayPlataform()) {
            hideInnerComponents();
            return;
        }

		ResponseWriter writer =
			context.getResponseWriter();
        writer.flush();
	}

    @Override
    public void encodeEnd(FacesContext context)
            throws IOException {
        if (!displayPlataform())
            return;
        context.getExternalContext().getRequestMap().remove("barLocModifier");
        ResponseWriter writer = context.getResponseWriter();
        writer.flush();
//        return;
    }

    private void setDisplayPlataform(String target){
        String[] targets = target.split(" ");
        super.setDisplayMobile(false);
        super.setDisplayTv(false);
        super.setDisplayWeb(false);
        for (String s : targets){
            if (s.equalsIgnoreCase("mobile"))
                super.setDisplayMobile(true);
            else if (s.equalsIgnoreCase("tvd"))
                super.setDisplayTv(true);
            else if (s.equalsIgnoreCase("web"))
                super.setDisplayWeb(true);
        }
    }

    @Override
    public void decode(FacesContext context) {
    }

    // <editor-fold defaultstate="collapsed" desc="Getters and Setters">
    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
    // </editor-fold>

}
